package com.链表;


/**
 * 自定义链表
 */

public class LinkNode{

        //头
        private  Node head;
        //数量
        private int count = 0 ;
        //索引
        private int index = 0;
        //转化成数组
        private String[] retArray;

        /**
         * 递归添加
         * @param data
         */
        public void add(String data){
            //不能添加空元素
            if(null == data){
                return;
            }
            Node newNode = new Node(data);
            if(this.head == null){
                this.head = newNode;
            }else{
                this.head.addNode(newNode);
            }
            //自增
            this.count ++;
        }

        /**
         * 获取数量
         * @return
         */
        public int getSize(){
            return this.count;
        }

        /**
         * 判断链表是否为空
         * @return
         */
        public boolean isEmpty(){
            return 0 == this.count;
        }

        /**
         * 递归查询
         * @param data
         * @return
         */
        public boolean contains(String data){
            if(null == data || null == this.head){
                return false;
            } else{

                return this.head.containNode(data);}
        }

        /**
         * 打印链表
         */
        public void print(){
            if(this.head != null){
                this.head.pringNode();
            }
        }

    /**
     * 根据索引查找内容
     * @param index
     * @return
     */
    public String getNode(int index){
            //判断索引是否越界
            if(index >= this.count){
                return null;
            }
            this.index = 0;
            return this.head.getNode(index);

        }
        public boolean setNode(int index,String data){
            if(index > this.index){
                return false;
            }
            this.index = 0 ;
            return this.head.setNode(index,data);
        }

    /**
     * 删除节点
     * @param data
     */
    public void removeNode(String data){
        if(this.contains(data)){
            if(data.equals(this.head.data)){
                this.head = this.head.next;
            }else{
                this.head.next.removeNode(this.head,data);
            }
            this.count --;
        }
        }
        /////////////////////////////////////////////////////
        //节点类
        class Node{
            //数据
            private String data;
            //指向下一个节点
            private Node next;
            //构造器
            public Node(String data){
                this.data = data;
            }

            /**
             * 递归添加节点
             * @param node
             */
            public void addNode(Node node){

                if(this.next == null){
                    this.next = node;
                }else{
                    this.next.addNode(node);
                }
            }

            /**
             * 递归查询
             * @param data
             * @return
             */
            public boolean containNode(String data){
                if(data.equals(this.data)){
                    return true;
                }else{
                    //判断下一个连表是否为空
                    if(null != this.next){
                        return this.next.containNode(data);
                    }else{
                        return false;
                    }
                }
            }

            /**
             * 递归打印节点
             */
            public void pringNode(){
                System.out.println(data);
                if(this.next != null){
                    this.next.pringNode();
                }
            }

            /**
             * 根据索引查找链表
             * @param index
             * @return
             */
            public String getNode(int index){
                //LinkNode.this.index获取外部类属性的方法
                if(LinkNode.this.index ++ == index){
                    return this.data;
                }else{
                    return this.next.getNode(index);
                }
            }

            /**
             * 根据索引修改内容
             * @param index
             * @param data
             * @return
             */
            public boolean setNode(int index, String data) {
                if(LinkNode.this.index ++ == index){
                    this.data = data;
                    return true;
                }else{
                    return this.next.setNode(index,data);
                }
            }

            /**
             * 删除节点
             * @param preNode
             * @param data
             */
            public void removeNode(Node preNode,String data){
                if(data.equals(this.data)){
                    preNode.next = this.next;
                }else{
                    this.next.removeNode(this,data);
                }
            }

            /**
             * 转化成数组
             */
            public void toArrayNode(){
                LinkNode.this.retArray[LinkNode.this.count++] = this.data;
                if(null != this.next){
                    this.next.toArrayNode();
                }
            }

            public String getData() {
                return data;
            }

            public void setData(String data) {
                this.data = data;
            }

            public Node getNext() {
                return next;
            }

            public void setNext(Node next) {
                this.next = next;
            }


        }

    public static void main(String[] args) {

        System.out.println("============添加============");
        LinkNode link = new LinkNode();
        link.add("one");
        link.add("two");
        link.add("three");
        link.print();
        System.out.println(link.getSize());
        System.out.println(link.isEmpty());
        System.out.println(link.contains("three"));
        System.out.println(link.getNode(2));
        System.out.println(link.setNode(2,"妈逼"));
        System.out.println(link.getNode(2));
       link.removeNode("妈逼");
        System.out.println(link.getNode(2));


    }
}
